package cn.neusoft.controller;

import cn.neusoft.domain.Substation;
import cn.neusoft.domain.Task;
import cn.neusoft.enums.Role;
import cn.neusoft.enums.TaskState;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.feignClients.AdminFeignClient;
import cn.neusoft.feignClients.DeliveryFeignClient;
import cn.neusoft.query.OperatorQuery;
import cn.neusoft.query.PaymentQuery;
import cn.neusoft.query.TaskQuery;
import cn.neusoft.service.SubstationService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/sub", produces = "application/json;charset=utf-8")
@Api(value = "分站的api", description = "分站相关功能")
public class SubstationController {

    @Autowired
    SubstationService substationService;

    @Autowired
    DeliveryFeignClient deliveryFeignClient;

    @Autowired
    AdminFeignClient adminFeignClient;

    @PostMapping("/taskPage")
    @ApiOperation("分页模糊查询任务单")
    public ResultBean findTaskByPage(@RequestBody TaskQuery taskQuery) {

        if(taskQuery == null
                || StringUtils.isEmpty(taskQuery.getPageNow())
                || StringUtils.isEmpty(taskQuery.getPageSize())
                || taskQuery.getSubstation_id() == null
        ){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        return deliveryFeignClient.findByPage(taskQuery);
    }

    @PostMapping("/assignTask")
    @ApiOperation("分配任务")
    public ResultBean AssignmentTask(@RequestBody Task task){

        if (task == null
                || task.getTask_id() == null
                || task.getOperator_id() == null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        substationService.assignTask(task);

        return ResultBean.success();
    }

    @GetMapping("/deliveryReceipt/{task_id}")
    @ApiOperation("生成配送签收单")
    public ResultBean generateDeliveryReceipt(@PathVariable("task_id") String task_id){

        if (StringUtils.isEmpty(task_id)){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        Task task = substationService.generateDeliveryReceipt(task_id);

        return ResultBean.success(task);
    }

    @PostMapping("/entryReceipt")
    @ApiOperation("回执录入")
    public ResultBean entryReceipt(@RequestBody Task task){

        if (task == null
                || StringUtils.isEmpty(task.getTask_id())
                || StringUtils.isEmpty(task.getRemark())){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        substationService.entryReceipt(task);

        return ResultBean.success();
    }

    @GetMapping("/findTaskById/{task_id}")
    @ApiOperation("根据id查询任务单")
    public ResultBean findTaskById(@PathVariable("task_id") String task_id){

        if (StringUtils.isEmpty(task_id)){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        return deliveryFeignClient.findById(task_id);
    }


    @PostMapping("/findOperator")
    @ApiOperation("分页模糊查询本分站的配送员")
    public ResultBean findOperator(@RequestBody OperatorQuery operatorQuery) {

        if(operatorQuery == null
                || StringUtils.isEmpty(operatorQuery.getPageNow())
                || StringUtils.isEmpty(operatorQuery.getPageSize())
                || operatorQuery.getDept_id() == null
        ){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        operatorQuery.setRole_id(Role.DELIVERY.getCode());

        return adminFeignClient.findByPage(operatorQuery);
    }

    @PostMapping("/inquiryPayment")
    @ApiOperation("缴款查询")
    public ResultBean inquiryPayment(@RequestBody PaymentQuery paymentQuery) {

        if(paymentQuery == null
                || StringUtils.isEmpty(paymentQuery.getPageNow())
                || StringUtils.isEmpty(paymentQuery.getPageSize())
        ){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        PageBean pageBean = substationService.inquiryPayment(paymentQuery);

        return ResultBean.success(pageBean);
    }

}
